package com.jx.gocomtask.webservice.entity.listener;


import java.util.Date;

import javax.persistence.*;

import com.jx.gocomtask.webservice.DTO.CallMeStatus;
import com.jx.gocomtask.webservice.DTO.CycleRun;
import com.jx.gocomtask.webservice.DTO.ScheduleStatus;
import com.jx.gocomtask.webservice.DTO.WorkStatus;
import com.jx.gocomtask.webservice.entity.ExtTaskExtension;
import com.jx.gocomtask.webservice.entity.ExtTaskUser;
import org.springframework.web.client.RestTemplate;
import com.jx.gocomtask.SpringContextUtils;
import com.jx.gocomtask.webservice.entity.ExtTaskMessage;
import com.jx.gocomtask.webservice.service.Route;
import com.jx.gocomtask.webservice.service.SystemSuperApp;
public class ExtTaskMessageListener {
	private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ExtTaskMessageListener.class);
	private RestTemplate restTemplate;
	private SystemSuperApp systemSuperApp;
	private String defaultAppId;
	private String defaultGroupAppId;
	private String gocomeApiUrl;
	private Route route;
	
	@PrePersist
	public void prePersist(Object entity) {
		if (entity.getClass() ==ExtTaskMessage.class){
			ExtTaskMessage extTaskMessage=(ExtTaskMessage)entity;
			resetStatus(extTaskMessage);
		}
	}

	@PreUpdate
	public void preUpdate(Object entity) {
		if (entity.getClass() ==ExtTaskMessage.class){
			ExtTaskMessage extTaskMessage=(ExtTaskMessage)entity;
			resetStatus(extTaskMessage);
		}
	}
	@PostPersist
	 public void postPersist(Object entity) {
		if (entity.getClass() ==ExtTaskMessage.class){
			ExtTaskMessage extTaskMessage=(ExtTaskMessage)entity;
			handel(extTaskMessage);
		}
	}
	
	private void resetStatus(ExtTaskMessage message){
		if(message.getExecPlan()==ScheduleStatus.LIGTH){
			message.setIsRemind(CallMeStatus.NO);
			message.setIsRepeat(CycleRun.NO);
			message.setDeadline(null);
			message.setWorkMode(WorkStatus.NO);
			message.setLabel(null);
		}
	}

//	创建时-统计处理
	public static void handel(ExtTaskMessage message) {
		if(null == message || null == message.getExtTaskUser()
				|| message.getExtTaskUser().size() == 0)
			return;
		ExtTaskExtension extension = new ExtTaskExtension();
		int start = 0, received = 0, finished = 0,refused = 0;
		for(ExtTaskUser user : message.getExtTaskUser()){
			switch (user.getStatus()) {
				case START: start += 1; break;
				case RECEIVED: received += 1;break;
				case FINISHED: finished += 1;break;
				case REFUSE: received += 1;break;
			}
		}
		extension.setMessageId(message.getId());
		extension.setStartNumber(start);
		extension.setReceivedNumber(received);
		extension.setFinishedNumber(finished);
		extension.setRefuseNumber(refused);

		message.setExtTaskExtension(extension);
	}

	
	private void updateUrl( ExtTaskMessage extTaskMessage){
		String uuid=extTaskMessage.getUuid();
		extTaskMessage.setUrl(extTaskMessage.getUrl()+"&taskId="+uuid);
		extTaskMessage.setIos(extTaskMessage.getIos()+"&taskId="+uuid);
		extTaskMessage.setAndroid(extTaskMessage.getAndroid()+"&taskId="+uuid);
	}
	
	public RestTemplate getRestTemplate() {
		if(this.restTemplate==null){
			this.restTemplate=SpringContextUtils.getBeanByClass(RestTemplate.class);
		}
		return restTemplate;
	}
	public SystemSuperApp getSystemSuperApp() {
		if(this.systemSuperApp==null){
			this.systemSuperApp=SpringContextUtils.getBeanByClass(SystemSuperApp.class);
		}
		return systemSuperApp;
	}
	public String getDefaultAppId() {
		if(this.defaultAppId==null){
			this.defaultAppId=SpringContextUtils.getBeanById("defaultAppId", String.class);
		}
		return defaultAppId;
	}
	public String getGocomeApiUrl() {
		if(this.gocomeApiUrl==null){
			this.gocomeApiUrl=SpringContextUtils.getBeanById("apiurl", String.class);
		}
		return gocomeApiUrl;
	}
	public String getDefaultGroupAppId() {
		if(this.defaultGroupAppId==null){
			this.defaultGroupAppId=SpringContextUtils.getBeanById("defaultGroupAppId", String.class);
		}
		return defaultGroupAppId;
	}
	public Route getRoute() {
		if(this.route==null){
			this.route=SpringContextUtils.getBeanByClass(Route.class);
		}
		return route;
	}
}
